
#########################################################################

# Copyright (C) Flavio Piccinelli 2012 <flavio.picci@gmail.com>

#########################################################################

# hostLoop.rb
# This file is part of "AriaAperta".

# AriaAperta is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# AriaAperta is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AriaAperta.  If not, see <http://www.gnu.org/licenses/>.

#########################################################################
#########################################################################


#########################################################################
# Required libraries
#########################################################################
require 'rubygems'
require 'sequel'
#########################################################################

HOSTNAME = `hostname`.chomp + "." + `dnsdomainname`.chomp

db = Sequel.sqlite("stats.db")
db.create_table?(:hosts){
	String :name
	Float :bogo,:default => nil
	Float :cpu0,:default => nil
	Float :cpu1,:default => nil
	Float :cpu2,:default => nil
	Float :cpu3,:default => nil
	Float :cpu4,:default => nil
	Float :cpu5,:default => nil
	Float :cpu6,:default => nil
	Float :cpu7,:default => nil
	Float :cpu8,:default => nil
	Float :cpu9,:default => nil
	}

bogo = `cat /proc/cpuinfo | grep bogomips | awk {'sum+=$3;print sum'}
				| tail -n 1`.chomp.to_f
		
db[:hosts].filter(:name => HOSTNAME).delete
db[:hosts].insert(:name => HOSTNAME)
db[:hosts].filter(:name => HOSTNAME).update(:bogo => bogo)

indx = 0
loop do

	cpu = `ps aux | awk {'sum+=$3;print sum'} | tail -n 1`.chomp.to_f
	db[:hosts].filter(:name => HOSTNAME).update(:"cpu#{indx}" => cpu)
	indx = (indx+1)%10
 	sleep 30

end


puts HOSTNAME
